/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package input;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;

import toolz.IMS_CSV_File;
import toolz.ImageMethods;
import DATA.iDataSetParts.Measurement;

/**
 * 
 * @author Bunkowski
 */
public class HEATMAP_BUFFER_IO {

	public static void saveToCache(Vector<?> wrap, String cachedFileName) {
		ObjectOutputStream oo = null;
		try {
			if (!(new File("ChartCache").exists())) {
				new File("ChartCache").mkdir();
			}
			oo = new ObjectOutputStream(new FileOutputStream("ChartCache/" + cachedFileName));
			oo.writeObject(wrap);
			oo.close();
			System.out.println("Adding " + cachedFileName + " to cache");
		} catch (Exception ex) {
			try {
				oo.close();
			} catch (IOException ex1) {
				System.out.println("Error, can not close damaged object out stream");
			}
			ex.printStackTrace();
			System.out.println("Error writing CCA");
		}
	}

	public static boolean fileInCache(String filename) {
		if (!(new File("ChartCache").exists())) {
			return false;
		}
		if (new File("ChartCache/" + filename.replace(".csv", ".cca")).exists()) {
			return true;
		}
		return false;
	}

	public static Vector<?> loadFromCache(Measurement measurement) {
		Vector<?> wrap = new Vector<Object>();
		if (new File("ChartCache/" + new File(measurement.path).getName().replace(".csv", ".cca")).exists()) {
			ObjectInputStream oi = null;
			try {
				oi = new ObjectInputStream(new FileInputStream(new File("ChartCache/"
						+ new File(measurement.path).getName().replace(".csv", ".cca"))));
				wrap = (Vector<?>) oi.readObject();

				oi.close();
				System.out.println("Valid cache object found, loading ..");
			} catch (Exception ex) {
				try {
					oi.close();
				} catch (Exception ex1) {
					System.out.println("Warning, can not close damaged object in stream ...");
				}
				System.out.println(ex.getMessage());
				System.out.println("Error loading cache object ..");
			}
		}
		return wrap;
	}

	public static double[][] matrixTojfc(double[][] map, double[] scaleK0, double[] scaleRT) {
		double cutoff = 0.0020;
		int count = 0;
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				if (map[i][j] > cutoff) {
					count++;
				}
			}
		}

		System.out.println("nr of elements in map: " + count);

		double[][] jfcmap = new double[3][count];
		int idx = 0;
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[0].length; j++) {
				if (map[i][j] > cutoff) {
					jfcmap[0][idx] = scaleK0[i];
					jfcmap[1][idx] = scaleRT[j];
					jfcmap[2][idx] = map[i][j];
					idx++;

				}
			}
		}
		return jfcmap;
	}

	public static Vector<Object> loadFromFile(IMS_CSV_File ims_file, boolean useDTAxis) {

		System.out.println("Importing File " + ims_file.getFile().getName());
		double[][] map = ims_file.getMap();
		double[] scaleK0 = null;
		if (useDTAxis) {
			scaleK0 = ims_file.getScaleDT();
		} else {
			scaleK0 = ims_file.getScaleK0Inv();
		}

		double[] scaleRT = ims_file.getScaleRT();

		// map = ImageMethods.normArray(map);
		// try {
		// map = ImageMethods.baselineCorrectionSpectraWise(map);
		// } catch (Exception x) {
		// System.out.println("Spectrawise baselinecorrection failed, try to correct global");
		// try {
		// map = ImageMethods.baselineCorrection(map);
		// } catch (Exception x2) {
		// System.out.println("!!! ERROR !!! global baselinecorrection failed !!!");
		// }
		// }
		// try {
		// Vector wrap = ImageMethods.interpolateMissingSpectra(map, scaleRT);
		// map = (double[][]) wrap.get(0);
		// scaleRT = (double[]) wrap.get(1);
		// } catch (Exception x) {
		// System.out.println("Interpolation of missing spectra failed");
		// }

		map = ImageMethods.defaultFilterPipeline(map, true, true, true, true);

		// double[][] rawMap = map.clone();
		// map = ImageMethods.histogramEqualization(map);
		// double[][] rawMap = map;

		double cutoff = 0.0020;

		int count = 0;
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				if (map[i][j] > cutoff) {
					count++;
				}
			}
		}

		System.out.println("nr of elements in map: " + count);

		double[][] jfcmap = new double[3][count];
		double[][] rawDataMap = new double[3][count];
		int idx = 0;
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[0].length; j++) {
				if (map[i][j] > cutoff) {
					jfcmap[0][idx] = scaleK0[i];
					jfcmap[1][idx] = scaleRT[j];
					jfcmap[2][idx] = map[i][j];

					// rawDataMap[0][idx] = scaleK0[i];
					// rawDataMap[1][idx] = scaleRT[j];
					// rawDataMap[2][idx] = rawMap[i][j];

					idx++;

				}
			}
		}
		rawDataMap = jfcmap;// no raw computation needed anymore

		Vector<Object> wrap = new Vector<Object>();
		wrap.add(jfcmap);
		wrap.add(rawDataMap);
		wrap.add(scaleK0);
		wrap.add(scaleRT);
		System.out.println("WARNING!!: Heatmap_Buffer_io uses old method!");
		return (wrap);
	}

	public static Vector<Object> loadFromFileWithNoiselevel(IMS_CSV_File ims_file, double noiseLevel,
			boolean ripCompensation, boolean medianFilter, boolean sgFilter, boolean gaussFilter) {

		System.out.println("Importing File " + ims_file.getFile().getName());
		double[][] map = ims_file.getMap();
		double[] scaleK0 = null;
		scaleK0 = ims_file.getScaleK0Inv();
		double[] scaleRT = ims_file.getScaleRT();

		// try {
		// Vector wrap = ImageMethods.interpolateMissingSpectra(map, scaleRT);
		// map = (double[][]) wrap.get(0);
		// scaleRT = (double[]) wrap.get(1);
		// } catch (Exception x) {
		// System.out.println("Interpolation of missing spectra failed");
		// }

		map = ImageMethods.defaultFilterPipeline(map, ripCompensation, medianFilter, sgFilter, gaussFilter);

		double[][] rawMap = map.clone();

		double cutoff = noiseLevel;

		int count = 0;
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				if (map[i][j] > cutoff) {
					count++;
				}
			}
		}

		System.out.println("nr of elements in map: " + count);

		double[][] jfcmap = new double[3][count];
		double[][] rawDataMap = new double[3][count];
		int idx = 0;
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[0].length; j++) {
				if (map[i][j] > cutoff) {
					jfcmap[0][idx] = scaleK0[i];
					jfcmap[1][idx] = scaleRT[j];
					jfcmap[2][idx] = map[i][j];

					rawDataMap[0][idx] = scaleK0[i];
					rawDataMap[1][idx] = scaleRT[j];
					rawDataMap[2][idx] = rawMap[i][j];

					idx++;

				}
			}
		}

		Vector<Object> wrap = new Vector<Object>();
		wrap.add(jfcmap);
		wrap.add(rawDataMap);
		wrap.add(scaleK0);
		wrap.add(scaleRT);
		return (wrap);
	}
}
